import { parseTimeDot } from '@/utils/utils.js'
import { baseUrl } from '@/utils/config.js'
import { getInfo, collect } from '@/api/message.js'
var setTime = null
export default {
	data() {
		return {
			parseTimeDot,
			baseUrl,
			user: {}, // 用户信息
			isFocus: false, // 输入框焦点
			msg: '',
			blog: [], //删除评论
			dataList: [], // 详情页数据
			commentList: [], //评论列表
			showMore: false, //分享弹层
			showFontSize: false, // 字体弹出
			fontSize: '28rpx',
			showParse: true, // 富文本显示状态
			fontSizeIndex: 0,
			commentText: '', // 输入的评论内容
			id: '',
			is_collect: 0,
			tagStyle: {
				table: 'box-sizing: border-box; border-top: 1px solid #dfe2e5; border-left: 1px solid #dfe2e5;',
				th: 'border-right: 1px solid #dfe2e5; border-bottom: 1px solid #dfe2e5;',
				td: 'border-right: 1px solid #dfe2e5; border-bottom: 1px solid #dfe2e5;',
				li: 'margin: 5px 0;'
			},
			// 更多菜单
			menuList: [{
				icon: '/static/img/wenTiKu/wx.png',
				name: '微信好友',
				remark: '',
				url: '',
				handler: 'shareTo'
			}, {
				icon: '/static/img/wenTiKu/pyq.png',
				name: '朋友圈',
				remark: '',
				url: '',
				handler: 'shareToTimeline'
        },
      //   {
			// 	icon: '/static/img/wenTiKu/qq.png',
			// 	name: 'QQ好友',
			// 	remark: '',
			// 	url: '',
			// 	handler: 'toShareQq'
			// }, {
			// 	icon: '/static/img/wenTiKu/copyLink.png',
			// 	name: '复制链接',
			// 	remark: '',
			// 	url: '',
			// 	handler: 'copyLink'
      //   },
        {
				icon: `/static/img/wenTiKu/collect_0.png`,
				name: '收藏',
				remark: '',
				url: '',
				handler: 'collect'
			}, {
				icon: '/static/img/wenTiKu/fontSize.png',
				name: '阅读字号',
				remark: '',
				url: '',
				handler: 'changShowFontSize'
			}],
			scrollTop: 0, // 滚动距离顶部的距离
			nScrollTop: 0, // 滚动的新top
			nScrollTop2: null,
			pScrollTop: 0, // 评论的top
			scrollIndex: 1,
		};
	},
	onLoad(e) {
		var that = this
		this.user = uni.getStorageSync('userInfo')
		this.id = e.id
		uni.onKeyboardHeightChange(res => {
			if (res.height == 0) {
				that.isFocus = false
			} else {
				that.isFocus = true
			}
		})
	},
	onShow() {
		this.getInfo()
	},
	onReady() {
		setTime = setInterval(() => {
			const query = uni.createSelectorQuery().in(this);
			query.select('#plus').boundingClientRect(data => {
				this.pScrollTop = data.height
			}).exec();
		}, 600)
	},
	onUnload() {
		clearInterval(setTime)
	},
	computed: {
		'fontSizeLeft': function () {
			return `primary${this.fontSizeIndex + 1}`
		}
	},
	methods: {
		//详情
		getInfo() {
			getInfo({ id: this.id }).then(res => {
				if (res.code == 1) {
					this.dataList = res.data
					this.is_collect = res.data.is_collect
					this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.is_collect}.png`
				}
			})

		},

		close() {
			this.showMore = false
		},

		setScrollTop() {
			if (this.scrollIndex % 2 == 0) {
				this.nScrollTop2 = null
				this.scrollTop = this.nScrollTop
			} else {
				this.scrollTop = this.pScrollTop
				this.nScrollTop2 = this.nScrollTop
			}
			this.scrollIndex++
		},

		getScrollTop(e) {
			if (this.nScrollTop2 == null)
				this.nScrollTop = e.detail.scrollTop
		},

		navigate(e) {
			this.download(e.href)
		},

		menuClick(item) {
			if (item.handler) {
				this[item.handler]()
			} else if (item.url) {
				this.$used.href(item.url)
			}
		},
		// 切换字体弹出
		changShowFontSize() {
			this.showFontSize = !this.showFontSize
			this.showMore = false
		},
		//分享到微信好友
		shareTo() {
			var share_url = this.$h5Url.server + '/pages/details/informationDetail?id=' + this.id
			console.log(share_url)
			uni.share({
				provider: "weixin",
				scene: "WXSceneSession",
				type: 0,
				href: share_url,
				title: this.dataList.title,
				summary: this.getHtmlText().substring(0, 100),
				imageUrl: "/static/logo-min.png",
				success: function (res) {
					console.log("success:" + JSON.stringify(res));
					console.log(res)
					if (callback != null) {
						callback()
					}
					this.showMore = false
				},
				fail: function (err) {
					console.log("fail:" + JSON.stringify(err));
				}
			});
		},
		//分享到微信朋友圈
		shareToTimeline() {
			var share_url = this.$h5Url.server + '/pages/details/informationDetail?id=' + this.id
			console.log(share_url)
			uni.share({
				provider: "weixin",
				scene: "WXSceneTimeline",
				type: 0,
				href: share_url,
				title: this.dataList.title,
				summary: this.getHtmlText().substring(0, 100),
				imageUrl: "/static/logo-min.png",
				success: function (res) {
					console.log("success:" + JSON.stringify(res));
					console.log(res)
					if (callback != null) {
						callback()
					}
					this.showMore = false
				},
				fail: function (err) {
					console.log("fail:" + JSON.stringify(err));
				}
			});
		},
		// 分享到qq
		toShareQq() {
			var share_url = this.$h5Url.server + '/pages/details/informationDetail?id=' + this.id
			console.log(share_url)
			uni.share({
				provider: "qq",
				scene: "WXSceneSession",
				type: 0,
				href: share_url,
				title: this.dataList.title,
				summary: this.getHtmlText().substring(0, 100),
				imageUrl: "/static/logo-min.png",
				success: function (res) {
					console.log("success:" + JSON.stringify(res));
					console.log(res)
					if (callback != null) {
						callback()
					}
					this.showMore = false
				},
				fail: function (err) {
					console.log("fail:" + JSON.stringify(err));
				}
			});
		},
		//复制链接
		copyLink() {
			let _that = this
			uni.setClipboardData({
				data: _that.$h5Url.server + '/pages/details/informationDetail?id=' + this.id,
				success: function () {
					uni.showToast({
						title: '链接复制成功'
					})
					_that.showMore = false
					uni.showToast({
						title: '链接复制成功'
					})
				}
			});
		},

		//收藏
		collect() {
			collect({
				cid: this.id
			}).then(res => {
				if (res.code == 1) {
					uni.showToast({
						title: res.data,
						icon: 'none'
					})
					this.is_collect = this.is_collect ? 0 : 1
					this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.is_collect}.png`
				}
			})
		},

		//改变字体
		changFontSize(index) {
			this.fontSizeIndex = index
			switch (index) {
				case 0:
					this.fontSize = '28rpx'
					break
				case 1:
					this.fontSize = '36rpx'
					break
				case 2:
					this.fontSize = '46rpx'
					break
			}
			this.showParse = false
			this.$nextTick(() => {
				this.showParse = true
			})
		},

		//签订合同下载
		download(e) {
			uni.downloadFile({
				url: e, //下载地址接口返回
				success: (data) => {
					if (data.statusCode === 200) {
						//文件保存到本地
						uni.saveFile({
							tempFilePath: data.tempFilePath, //临时路径
							success: function (res) {
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '文件下载成功，2秒后将自动打开', //保存路径  res.savedFilePath
									duration: 3000,
								});
								setTimeout(() => {
									//打开文档查看
									uni.openDocument({
										filePath: res.savedFilePath,
										success: function (res) {
											// console.log('打开文档成功');
										}
									});
								}, 2000)
							}
						});
					}
				},
				fail: (err) => {
					console.log(err);
					uni.showToast({
						icon: 'none',
						mask: true,
						title: '失败，请重新下载',
					});
				},
			});
		},

		//获取富文本内容
		getHtmlText(str) {
			return str = this.dataList.content.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, "");
		}
	},
}